<?php
require_once '../../Model/Cliente.php';
require_once '../../DAO/DaoCadastrarUsuario.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $emailantiga = trim($_POST['emailantiga']);
    $novoemail   = trim($_POST['novoemail']);
    $novoemail2  = trim($_POST['novoemail2']);
    if ((empty($emailantiga) || strlen($emailantiga) < 6) || (!strpos($emailantiga, '@')) || (!strpos($emailantiga, '.'))) {
        $erro['emailantiga'] = "<font color='red' size='1'>Compo e-mail atual inv&aacute;lido</font>";
    }
    if ((empty($novoemail) || strlen($novoemail) < 6) || (!strpos($novoemail, '@')) || (!strpos($novoemail, '.'))) {
        $erro['novoemail'] = "<font color='red' size='1'>Compo novo e-mail inv&aacute;lido</font>";
    }    
    if ((empty($novoemail2) || strlen($novoemail2) < 6) || (!strpos($novoemail2, '@')) || (!strpos($novoemail2, '.'))) {
        $erro['novoemail2'] = "<font color='red' size='1'>Compo confirma e-mail inv&aacute;lido</font>";
    }  
    if (empty($novoemail) || empty($novoemail2) || ($novoemail != $novoemail2)) {
        $erro['novoemail2'] = "<font color='red' size='1'>E-mail diferente...</font>";
    }
    if (!isset($erro)) {       
        $cliente = new Cliente();
        $dao = new DaoCadastrarUsuario();
        $cliente->setEmail($novoemail);
        $dao->alterarEmail($cliente);
        echo '<script> alert("E-mail alterado com sucesso!.")</script>';
    }
}
?>
